package com.zdong.hintview;

import android.app.ActivityManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by user on 2015/10/13.
 */
public class WelComeService extends Service {


    private WindowManager.LayoutParams wmParams;
    private View welView;

    private void createView() {
        final WindowManager systemService = (WindowManager) this.getApplication().getSystemService(Context.WINDOW_SERVICE);
        wmParams = new WindowManager.LayoutParams();
        wmParams.type = WindowManager.LayoutParams.TYPE_PHONE;
        welView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
//        welView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
        welView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                systemService.removeView(welView);
                stopSelf();
            }
        });
//        wmParams.flags |= WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
        systemService.addView(welView, wmParams);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        createView();
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                while (true) {
//                    if (isHome()) {
////                        handler.sendEmptyMessage(1);
//                        startActivity(new Intent(WelComeService.this, MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
//                        break;
//                    }
//                    try {
//                        Thread.sleep(1000);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                }
//            }
//        }).start();
        return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        Log.e("TAG", "onCreate");
        welView = LayoutInflater.from(this).inflate(R.layout.hintlayout, null);
        super.onCreate();
    }

    /**
     * 判断当前界面是否是桌面
     */
    private boolean isHome() {
        ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> rti = mActivityManager.getRunningTasks(1);
        // 如果在桌面，输出的是桌面的包名
        // 不在桌面的话输出的是打开的app的包名
        Log.e("TAG", "getHomes().contains(rti.get(0).topActivity.getPackageName())+++" + rti.get(0).topActivity.getPackageName());
        return getHomes().contains(rti.get(0).topActivity.getPackageName());
    }

    /**
     * 获得属于桌面的应用的应用包名称
     *
     * @return 返回包含所有包名的字符串列表
     */
    private List<String> getHomes() {
        List<String> names = new ArrayList<String>();
        PackageManager packageManager = this.getPackageManager();
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(
                intent, PackageManager.MATCH_DEFAULT_ONLY);
        for (ResolveInfo ri : resolveInfo) {
            // 桌面包名
            Log.e("TAG", "ri.activityInfo.packageName++++" + ri.activityInfo.packageName);
            names.add(ri.activityInfo.packageName);
        }
        return names;
    }

}
